package com.ruoyi.data.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.data.domain.MyProcess;
import com.ruoyi.data.domain.MyRelationship;
import com.ruoyi.data.mapper.MyProcessMapper;
import com.ruoyi.data.service.MyProcessService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-10
 */
@Service
public class MyProcessServiceImpl implements MyProcessService
{
    @Autowired
    private MyProcessMapper myProcessMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param name 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public MyProcess selectMyProcessById(String name)
    {
        return myProcessMapper.selectMyProcessByName(name);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @return 【请填写功能名称】
     */
    @Override
    public Map<String, Object> selectMyProcessList()
    {
        //状态的模板
        JSONObject tem = new JSONObject();
        tem.put("color","red");

        HashMap<String, Object> res = new HashMap<>(16);
        List<MyProcess> myProcesses = myProcessMapper.selectMyProcessList();

        //添加节点状态颜色
        for (MyProcess myProcess : myProcesses) {
            if ("0".equals(myProcess.getStatus())){
                myProcess.setItemStyle(tem);
            }
        }


        List<MyRelationship> myRelationships = myProcessMapper.selectMyRelationship();

        res.put("pro",myProcesses);
        res.put("rel",myRelationships);

        return res;
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param myProcess 【请填写功能名称】
     * @return 结果
     */
    @Override
    @Transactional
    public int insertMyProcess(MyProcess myProcess)
    {
        int i = myProcessMapper.insertMyProcess(myProcess);
        MyRelationship myRelationship = new MyRelationship();

        BeanUtils.copyProperties(myProcess,myRelationship);

        if (i > 0){
            int ii = myProcessMapper.insertMyRelationship(myRelationship);
        }
        return i;

    }

    /**
     * 修改【请填写功能名称】
     *
     * @param myProcess 【请填写功能名称】
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMyProcess(MyProcess myProcess)
    {
        int i = myProcessMapper.updateMyProcess(myProcess);

        //修改关系中的节点名称
        int q1 = myProcessMapper.updateRelationshipNodeName1(myProcess.getPrimaryKey().split(",")[1],myProcess.getName());
        int q2 = myProcessMapper.updateRelationshipNodeName2(myProcess.getPrimaryKey().split(",")[1],myProcess.getName());

        return 1;
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteMyProcessByIds(String ids)
    {
        return myProcessMapper.deleteMyProcessByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteMyProcessById(Long id)
    {
        return myProcessMapper.deleteMyProcessById(id);
    }

    @Override
    public int insertMyRelationship(MyRelationship myRelationship) {
        return myProcessMapper.insertMyRelationship(myRelationship);
    }

    /**
     * 删除节点或者关系
     * @param name
     * @return 节点 or 关系
     */
    @Override
    public String deleteNodeOrRelationship(String name) {
        String res = "";
        if (name.contains(">")){
            //删除关系
            String[] s = name.split(">");
            int i = myProcessMapper.deleteRelationship(s[0].trim(),s[1].trim());
            if (i>0){
                res = "删除关系成功";
            }else {
                res = "删除关系失败";
            }
        }else {
            //删除节点
            int ii = myProcessMapper.deleteNode(name.trim());
            //删除节点的同时要将所有有关它的关系删除
            int ii1 = myProcessMapper.deleteRelationshipById(name.trim());
            if (ii>0 && ii1>0){
                res = "删除节点成功";
            }else {
                res = "删除节点失败";
            }
        }
        return res;
    }
}
